//
//  ViewController.m
//  02-UICollectionView的复习使用
//
//  Created by J on 15/11/13.
//  Copyright © 2015年 JZJ. All rights reserved.
//

#import "ViewController.h"
#import "ImageCell.h"


//collectionView  iOS6出现 xcode5 不能显示jpg



@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)NSMutableArray * images;
@property(nonatomic,weak)UICollectionView * collectionView;
@end

@implementation ViewController

static NSString  * const ID= @"image"; //static 只能在本文件访问   const 常量不能被修改

- (NSMutableArray *)images{
    
    if (!_images) {
        
        self.images = [[NSMutableArray alloc]init];
        
        for ( int i=1; i<=20; i++) {
            [self.images addObject:[NSString stringWithFormat:@"%d",i]];
        }
     
    }
    
    return _images;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGRect rect =CGRectMake(0, 100, 320, 320);
    
    UICollectionView * collectionView =[[UICollectionView alloc]initWithFrame:rect collectionViewLayout:[[UICollectionViewFlowLayout alloc] init] ];
    collectionView.delegate = self;
    collectionView.dataSource = self;
//    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    [collectionView registerNib:[UINib nibWithNibName:@"ImageCell" bundle:nil] forCellWithReuseIdentifier:ID];

    [self.view addSubview:collectionView];
   
    
}

#pragma mark -UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ImageCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor orangeColor];
    
    cell.image = self.images[indexPath.item];

    return cell;

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
